<?php

class countrylist {
	
	var $cache;
	
	function countrylist() {
	
	}
	
	function render_options($type = 'world', $selected = '') {
		$this->_parse_file($type);
		foreach($this->cache[$type] as $c => $n) {
			$sel = ($c == $selected) ? ' selected="selected"' : '';
			$out .= '<option value="'.$c.'"'.$sel.'>'.$n.'</option>';
		}
		return $out;
	}
	
	function get_by_code($code, $type = 'world') {
		$this->_parse_file($type);
		return $this->cache[$type][$code];
	}
	
	function _parse_file($type = 'world') {
		if (!$this->cache[$type]) {
			$f = file (dirname(__FILE__).DIRECTORY_SEPARATOR.$type.".txt");
			
			if (is_array($f)) {
				foreach($f as $couple) {
					list($c, $n) = explode(";", $couple);
					$this->cache[$type][trim($c)] = trim($n);
				}
			}
		}
	}
	
}

?>